//
//  NewsTableViewCell.m
//  UI10_CustomTableViewCell
//
//  Created by Hanlingyu on 16/7/27.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "NewsTableViewCell.h"
#import "UIView+Frame.h"

// .h中声明Modal类, 需要在.m 中引用Modal的头文件, 这样可以避免交叉引用
#import "NewSObject.h"
@interface NewsTableViewCell ()

@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descriptionLabel;
@property (nonatomic, strong) UILabel *numberCountsLabel;


@end

@implementation NewsTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
    
}

- (void)createView {
    
    _picImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
//    _picImageView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_picImageView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//    _titleLabel.backgroundColor = [UIColor yellowColor];
    _titleLabel.font = [UIFont systemFontOfSize:17 weight:2];
    [self.contentView addSubview:_titleLabel];
    
    _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//    _descriptionLabel.backgroundColor = [UIColor grayColor];
    _descriptionLabel.numberOfLines = 0;
    _descriptionLabel.font = [UIFont systemFontOfSize:15];
    _descriptionLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_descriptionLabel];
    
    _numberCountsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _numberCountsLabel.layer.cornerRadius = 4;
    _numberCountsLabel.layer.masksToBounds = YES;
    _numberCountsLabel.textAlignment = 1;
    _numberCountsLabel.backgroundColor = [UIColor lightGrayColor];
    [_descriptionLabel addSubview:_numberCountsLabel];
}

// 有内容/坐标改变时, 会触发该方法
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat topMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat middleMargin = 5;
    CGFloat titleLabelHeight = 30;
    
    _picImageView.frame = CGRectMake(leftMargin, topMargin, self.contentView.ltq_width / 3, self.contentView.ltq_height - topMargin - bottomMargin);
    CGFloat titleLabelX = self.contentView.ltq_x + _picImageView.ltq_width + middleMargin;
    _titleLabel.frame = CGRectMake(titleLabelX, 5, self.contentView.ltq_width - titleLabelX - rightMargin, titleLabelHeight);
    CGFloat descriptionLabelY = topMargin + _titleLabel.ltq_height + bottomMargin;
    _descriptionLabel.frame = CGRectMake(titleLabelX, descriptionLabelY, _titleLabel.ltq_width, self.contentView.ltq_height - descriptionLabelY - bottomMargin);
    CGFloat numberCountsLabelX = _descriptionLabel.ltq_width / 5 * 3;
    CGFloat numberCountsLabelWidth = _descriptionLabel.ltq_width / 5 * 2;
    CGFloat numberCountsLabelHeight = _descriptionLabel.ltq_height / 2;
// 第一种方案: 判断父视图是不是_descriptionLabel
//    if (_numberCountsLabel.superview == _descriptionLabel) {
//        _numberCountsLabel.frame = CGRectMake(numberCountsLabelX, numberCountsLabelHeight, numberCountsLabelWidth, numberCountsLabelHeight);
//        CGRect newRect = [_numberCountsLabel.superview convertRect:_numberCountsLabel.frame toView:self.contentView];
//        _numberCountsLabel.frame = newRect;
//        [self.contentView addSubview:_numberCountsLabel];
//    }
// 第二种方法: 改_numberCountsLabel的父视图
    [_descriptionLabel addSubview:_numberCountsLabel];
    _numberCountsLabel.frame = CGRectMake(numberCountsLabelX, numberCountsLabelHeight, numberCountsLabelWidth, numberCountsLabelHeight);
    CGRect newRect = [_numberCountsLabel.superview convertRect:_numberCountsLabel.frame toView:self.contentView];
    _numberCountsLabel.frame = newRect;
    [self.contentView addSubview:_numberCountsLabel];
}

// 重写set方法, 将外面数组中的Modal赋给对应的cell并在set方法中赋值
- (void)setNewsObject:(NewSObject *)newsObject {
    _newsObject = newsObject;
    _picImageView.image = [UIImage imageNamed:newsObject.newsImage];
    _titleLabel.text = newsObject.newsTitle;
    _descriptionLabel.text = newsObject.newsDescription;
    _numberCountsLabel.text = newsObject.newsNumberCount;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
